package housing.task2.stale.fileIO.jsn;

import housing.task1.actor.Renter;

import java.io.File;
import java.io.IOException;

/**
 * Serializes objects from "Housing Service" subject area in .jsn
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 30.11.2010
 * Time: 15:09:31
 */
public class Writer {
    private static File outFile;


    public static void writeRenter(Renter renter) {
        checkFile(outFile);


    }

    private static void checkFile(File file) {
        if (file == null) {
            File path = createDefaultLocation();
            createFile(path);
            setOutFile(file);
        } else if (!file.exists()) {
            createFile(file);
        }
    }

    private static File createDefaultLocation() {
        final String path = ".\\main\\resources\\jsn\\Renter.jsn";
        return new File(path);
    }

    private static void createFile(File file) {
        System.out.println("Creating a new file for serializing Renter");
        try {
            file.createNewFile();
            System.out.println("File " + file.getAbsolutePath() + " created");
        } catch (IOException e) {
            System.out.println("Failed create file " + file.getAbsolutePath() + "!");
            e.printStackTrace();
        }
    }

    public static File getOutFile() {
        return outFile;
    }

    public static void setOutFile(File outFile) {
        System.out.println("OutFile was set to " + getOutFile().getAbsolutePath());
        Writer.outFile = outFile;
    }
}//class AbstractWriter
